//===============================================================================
// Microsoft patterns & practices
// Guidance Explorer Offline Client
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================



using System;
using System.Collections;
using System.Windows.Forms;
using GuidanceExplorer.Controls;
using GuidanceExplorer.Model;

namespace GuidanceExplorer.Views
{
    /// <summary>
    /// Allows sorting of the list view using the information in the columns.
    /// </summary>
    public class GuidanceItemListViewComparer : IComparer
    {
        /// <summary>
        /// Keep the column collection.
        /// </summary>
        private Column[] _columns;

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="columns"></param>
        public GuidanceItemListViewComparer(ColumnCollection columns)
        {
            _columns = new Column[columns.Values.Count];
            columns.Values.CopyTo(_columns, 0);
            Comparison<Column> comp = delegate(Column x, Column y)
                {
                    return Comparer.DefaultInvariant.Compare(x.Position, y.Position);
                };
            Array.Sort<Column>(_columns, comp);
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="columns"></param>
        public GuidanceItemListViewComparer(Column[] columns)
        {
            _columns = new Column[columns.Length];
            columns.CopyTo(_columns, 0);
            Comparison<Column> comp = delegate(Column x, Column y)
                {
                    return Comparer.DefaultInvariant.Compare(x.Position, y.Position);
                };
            Array.Sort<Column>(_columns, comp);
        }

        public int Compare(object x, object y)
        {
            bool allNone = true;
            foreach (Column column in _columns)
            {
                if (column.Sort != SortOrder.None)
                {
                    allNone = false;
                    break;
                }
            }

            GuidanceItemListViewItem lvX, lvY;

            lvX = x as GuidanceItemListViewItem;
            lvY = y as GuidanceItemListViewItem;

            if (lvX == null || lvY == null)
            {
                if (lvX == null && lvY == null)
                {
                    return 0;
                }
                else if (lvX == null)
                {
                    return 1;
                }
                else if (lvY == null)
                {
                    return -1;
                }
            }

            foreach (Column column in _columns)
            {
                if ((column.Sort != SortOrder.None) || allNone)
                {
                    if (column.Source != "Date")
                    {
                        int compare = String.Compare(lvX.GuidanceItem[column.Source], lvY.GuidanceItem[column.Source]);
                        if (compare != 0)
                        {
                            if ((column.Sort == SortOrder.Ascending) || allNone)
                            {
                                return compare;
                            }
                            else if (column.Sort == SortOrder.Descending)
                            {
                                return compare * (-1);
                            }
                        }
                    }
                    else
                    {
                        int compare = DateTime.Compare(lvX.GuidanceItem.DateTime, lvY.GuidanceItem.DateTime);
                        if (compare != 0)
                        {
                            if (column.Sort == SortOrder.Descending || allNone)
                            {
                                return compare * (-1);
                            }
                            else if (column.Sort == SortOrder.Ascending)
                            {
                                return compare;
                            }
                        }
                    }
                }
            }
            return 0;
        }

        /// <summary>
        /// Keep the column collection.
        /// </summary>
        public Column[] Columns
        {
            get { return _columns; }
            set { _columns = value; }
        }

    }

}


